<?php

function dbConnect($data){
	$connection = mysql_connect($data['HOST'], $data['USER'], $data['PASS']);
}

//this function returns the y of the free nodes at a given time space
function getFreeNodes($start, $end, $connection){
	//first connect to database
	dbConnect($connection);
	
	//keep the results in arrays
	$node_id = array();
	$nodes = array();
		
	//create the time state where he have a possible cut with the given start and end time
	$forbidden_time_state = "((('" . $start . "'>=begin_time) and ('" . $end . "'<=end_time)) or (('" . $start . "'>=begin_time) and ('" . $start . "'<end_time)) or (('" . $end . "'>=begin_time) and ('" . $end . "'<=end_time)) or (('" . $start . "'<=begin_time) and ('" . $end . "'>=end_time)))";
	
	//get the reserved nodes and the reservation ids at that time
	$sql_reservation_query = "SELECT * FROM `".$connection['SCHEDULER_DB']."`.`reservation` WHERE ".$forbidden_time_state."";
	$reservation_result = mysql_query($sql_reservation_query);
	while ($reservation_row = mysql_fetch_array($reservation_result)){
		$node_id[] = $reservation_row['node_id'];
	}

	//get the nodes (y) that do not participate in the above reservations
	$s_node_id = implode(",", $node_id);
	if(count($node_id)==0){ $s_node_id=0; }
	$sql_node_y_query = "SELECT `y`,`nodes`.`id`,`nodes`.`hostname`,`nodes`.`control_ip` FROM `".$connection['INVENTORY_DB']."`.`locations`,`".$connection['INVENTORY_DB']."`.`nodes` WHERE `nodes`.`id` not in (".$s_node_id.") AND `nodes`.`location_id` = `locations`.`id`";
	$node_y_results = mysql_query($sql_node_y_query);
	while ($node_y_row = mysql_fetch_array($node_y_results))
	{
		$nodes[] = array('id' => $node_y_row['id'], 'hostname' => $node_y_row['hostname'], 'control_ip' => $node_y_row['control_ip']);
	}
							
	return $nodes;
}

//this function returns true if the node is not registered by a user between a given begin time (start) and end time (end) based on node id
function isNodeAvailable_id($start, $end, $node_id, $connection){
	//let's suppose that the node is available
	$retval = 'true';
		
	//connect o database
	dbConnect($connection);
	
	//get the forbidden time as defined in previous functions
	$forbidden_time_state = "((('" . $start . "' >= `begin_time`) AND ('" . $end . "'<= `end_time`)) OR (('" . $start . "' >= `begin_time`) AND ('" . $start . "' < `end_time`)) OR (('" . $end . "' >= `begin_time`) AND ('" . $end . "' <= `end_time`)) OR (('" . $start . "' <= `begin_time`) AND ('" . $end . "' >= `end_time`)))";
	
	//check for any reservations at that forbidden time where the specified node takes a part
	$sql_reservation_query = "SELECT `id` FROM `".$connection['SCHEDULER_DB']."`.`reservation` WHERE `node_id` = '".$node_id."' AND ".$forbidden_time_state."";

	$reservation_results = mysql_query($sql_reservation_query);
	while ($reservation_row = mysql_fetch_array($reservation_results)){
		//if we enter here, then a record exists and we should return false
		$retval = 'false';
	}
	
	return $retval;
}

function CreateNodeReservation($data, $connection){
	//connect to database
	dbConnect($connection);	
	
	//Reserve
	$sql_insert_reservation = "INSERT INTO `".$connection['SCHEDULER_DB']."`.`reservation` (username, begin_time, end_time, node_id) values ('".$data['user']."', '".$data['begin_time']."', '".$data['end_time']."', ".$data['node'].")";
	$insert = mysql_query($sql_insert_reservation);
	
	if(!$insert) return false;
	
	return true;
}

function CreateChannelReservation($data, $connection){
	//connect to database
	dbConnect($connection);	
	
	//Reserve
	$sql_insert_spec_reservation = "INSERT INTO `".$connection['SCHEDULER_DB']."`.`spec_reserve` (username, begin_time, end_time, spectrum_id) values ('".$data['user']."', '".$data['begin_time']."', '".$data['end_time']."', ".$data['channel'].")";
	$insert = mysql_query($sql_insert_spec_reservation);
	
	if(!$insert) return false;
	
	return true;
}
	
//this function cancels a reservation based on the reservation id
function cancelReservation_Id($reservation_id, $connection){
	//connect to database
	dbConnect($connection);
	
	//remove the reservation from the reservation table
	$sql_delete_reservation_query = "DELETE FROM `".$connection['SCHEDULER_DB']."`.`reservation` WHERE `id` = " . $reservation_id;
	mysql_query($sql_delete_reservation_query);
}

function cancelSpecReservation_Id($spec_reserve_id, $connection){
	//connect to database
	dbConnect($connection);
	
	//Remove the reservation from the spec_reserve table
	$sql_delete_reservation_query = "DELETE FROM `".$connection['SCHEDULER_DB']."`.`spec_reserve` WHERE `id` = " . $spec_reserve_id;
	mysql_query($sql_delete_reservation_query);
}

function getFreeSpectrum_a($start, $end, $connection){
	//first connect to database
	dbConnect($connection);
	
	//keep the results in arrays
	$spec_id = array();
	$specs = array();
	
	//create the time state where he have a possible cut with the given start and end time
	$forbidden_time_state = "((('" . $start . "'>=begin_time) and ('" . $end . "'<=end_time)) or (('" . $start . "'>=begin_time) and ('" . $start . "'<end_time)) or (('" . $end . "'>=begin_time) and ('" . $end . "'<=end_time)) or (('" . $start . "'<=begin_time) and ('" . $end . "'>=end_time)))";
		
	//get the reserved spectrums at that time
	$sql_reservation_query = "SELECT DISTINCT `spectrum_id` FROM `".$connection['SCHEDULER_DB']."`.`spec_reserve` WHERE " . $forbidden_time_state;
	$reservation_result = mysql_query($sql_reservation_query);
	while ($reservation_row = mysql_fetch_array($reservation_result)){
		$spec_id[] = $reservation_row['spectrum_id'];
	}

	//get ID's that do not participate in the above reservations
	$s_spec_id = implode(",", $spec_id);
	if(count($spec_id)==0){ $s_spec_id=0; }
	$sql_spec_ch_query = "select `id` FROM `".$connection['INVENTORY_DB']."`.`spectrum` WHERE `id` not in (" . $s_spec_id . ") and `modulation` = 'IEEE802_11a'";
	$spec_ch_results = mysql_query($sql_spec_ch_query);
	while ($spec_ch_row = mysql_fetch_array($spec_ch_results)){
		$specs[] = $spec_ch_row['id'];
	}
						
	return $specs;
}

function getFreeSpectrum_g($start, $end, $connection){
	//first connect to database
	dbConnect($connection);
	
	//keep the results in arrays
	$spec_id = array();
	$specs = array();
	
	//create the time state where he have a possible cut with the given start and end time
	$forbidden_time_state = "((('" . $start . "'>=begin_time) and ('" . $end . "'<=end_time)) or (('" . $start . "'>=begin_time) and ('" . $start . "'<end_time)) or (('" . $end . "'>=begin_time) and ('" . $end . "'<=end_time)) or (('" . $start . "'<=begin_time) and ('" . $end . "'>=end_time)))";
	
	//get the reserved spectrums at that time
	$sql_reservation_query = "SELECT DISTINCT `spectrum_id` FROM `".$connection['SCHEDULER_DB']."`.`spec_reserve` WHERE " . $forbidden_time_state;
	$reservation_result = mysql_query($sql_reservation_query);
	while ($reservation_row = mysql_fetch_array($reservation_result)){
		$spec_id[] = $reservation_row['spectrum_id'];
	}
	//get ID's that do not participate in the above reservations
	$s_spec_id = implode(",", $spec_id);
	if(count($spec_id)==0){ $s_spec_id=0; }
	$sql_spec_ch_query = "SELECT `id` FROM `".$connection['INVENTORY_DB']."`.`spectrum` WHERE `id` not in (" . $s_spec_id . ") AND `modulation` = 'IEEE802_11b_g'";
	$spec_ch_results = mysql_query($sql_spec_ch_query);
	while ($spec_ch_row = mysql_fetch_array($spec_ch_results)){
		$specs[] = $spec_ch_row['id'];
	}
						
	return $specs;
}

function getMyNodes($user,$connection){
	//connect o database
	dbConnect($connection);
	
	//keep the results in arrays
	$my_nodes = array();
	
	//Get Now date
	$now_date = date("Y-m-d H:i:s", mktime(date("H"), date("i")+5, 0, date("m"), date("d"), date("Y")));
	
	//Search for my nodes
	$sql_reserved_nodes_query = "SELECT * from `".$connection['SCHEDULER_DB']."`.`reservation` WHERE `username` = '" . $user . "' AND `end_time` >= '" . $now_date . "' ORDER BY `begin_time` DESC";
	$sql_reserved_nodes_results = mysql_query($sql_reserved_nodes_query);
		
	//Get results
	while ($sql_reserved_nodes_row = mysql_fetch_array($sql_reserved_nodes_results)){
		$sql_node_query = "SELECT `hostname`,`control_ip` FROM `".$connection['INVENTORY_DB']."`.`nodes` WHERE `id` = " . $sql_reserved_nodes_row['node_id'];
		$sql_node_results = mysql_query($sql_node_query);
		$sql_node_row = mysql_fetch_array($sql_node_results);
		
		$my_nodes[] = array(
						'node_id' => $sql_reserved_nodes_row['node_id'],
						'reservation_id' => $sql_reserved_nodes_row['id'],
						'begin_time' => $sql_reserved_nodes_row['begin_time'],
						'end_time' => $sql_reserved_nodes_row['end_time'],
						'node_name' => $sql_node_row['hostname'],
						'control_ip' => $sql_node_row['control_ip']
					);
	}
	
	return $my_nodes;
}

function getMyChannels($user,$connection){
	//connect o database
	dbConnect($connection);
	
	//keep the results in arrays
	$my_channels = array();
	
	//Get Now date
	$now_date = date("Y-m-d H:i:s", mktime(date("H"), date("i")+5, 0, date("m"), date("d"), date("Y")));
	
	//Search for my Channels
	$sql_reserved_specs_query = "SELECT * from `".$connection['SCHEDULER_DB']."`.`spec_reserve` WHERE `username` = '" . $user . "' AND `end_time` >= '" . $now_date . "' ORDER BY `begin_time` DESC";
	$sql_reserved_specs_results = mysql_query($sql_reserved_specs_query);
		
	//Get results
	while ($sql_reserved_specs_row = mysql_fetch_array($sql_reserved_specs_results)){
		$sql_spec_query = "SELECT `channel` FROM `".$connection['INVENTORY_DB']."`.`spectrum` WHERE `id` = " . $sql_reserved_specs_row['spectrum_id'];
		$sql_spec_results = mysql_query($sql_spec_query);
		$sql_spec_row = mysql_fetch_array($sql_spec_results);
		
		$my_channels[] = array(
						'spectrum_id' => $sql_reserved_specs_row['spectrum_id'],
						'id' => $sql_reserved_specs_row['id'],
						'begin_time' => $sql_reserved_specs_row['begin_time'],
						'end_time' => $sql_reserved_specs_row['end_time'],
						'channel' => $sql_spec_row['channel']
					);
	}
	
	return $my_channels;
}

?>
